import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '../layout/index.vue'
import nestedRouter from './modules/nested'

// v3 routes 命名限制
const routes = [{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../views/dashboard/dashboard.vue'),
    meta: { icon: 'dashboard', title: 'Dashboard' }
  }]
},
nestedRouter,
{
  path: '/login',
  name: 'login',
  component: () => import('../views/login/login.vue'),
  meta: { icon: '', title: 'login' },
  hidden: true
}]

const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes
})

export const setupRouter = (app) => {
  app.use(router)
}

export const resetRouter = () => {
  const resetWhiteNameList = ['Login']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name)) {
      router.removeRoute(name)
    }
  })
}

export default router
